package NewCoder;

public class BM37 {
    public int lowestCommonAncestor (TreeNode root, int p, int q) {
        // write code here
        if(root==null) return -1;
        if((p <= root.val && q >= root.val) || (p >= root.val && q <= root.val)){
            return root.val;
        }else if(p <= root.val && q <= root.val){
            return lowestCommonAncestor(root.left,p,q);
        }else{
            return lowestCommonAncestor(root.right,p,q);
        }
    }
}
